package cn.smbms.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.smbms.dao.BaseDao;
import cn.smbms.dao.BillMapper;
import cn.smbms.pojo.Bill;
import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.service.BillService;

@Transactional
@Service("billService")
public class BillServiceImpl implements BillService {
	@Autowired(required = false)
	private BillMapper billMapper;

	/*
	 * public BillServiceImpl() { billDao = new BillDaoImpl(); }
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean add(Bill bill) {
		// TODO Auto-generated method stub
		boolean flag = false;
		Connection connection = null;
		try {
			connection = BaseDao.getConnection();
			connection.setAutoCommit(false);// 开启JDBC事务管理
			if (billMapper.add(bill) > 0)
				flag = true;
			connection.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				System.out.println("rollback==================");
				connection.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} finally {
			// 在service层进行connection连接的关闭
			BaseDao.closeResource(connection, null, null);
		}
		return flag;
	}

 
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public List<Bill> getBillList(@Param("productName") String productName, @Param("providerId") Integer providerId,
			@Param("isPayment") Integer isPayment, @Param("from") Integer currentPageNo,
			@Param("pageSize") Integer pageSize) throws Exception {
		// TODO Auto-generated method stub
		Connection connection = null;
		List<Bill> billList = null;
		System.out.println("query productName ---- > " + productName);
		System.out.println("query providerId ---- > " + providerId);
		System.out.println("query isPayment ---- > " + isPayment);
		System.out.println("query currentPageNo ---- > " + currentPageNo);
		System.out.println("query pageSize ---- > " + pageSize);
		try {
			connection = BaseDao.getConnection();
			currentPageNo = (currentPageNo-1)*pageSize;
			System.out.println("service=="+ currentPageNo+" currentPageNo===================="+pageSize+"pageSize==========================");
			
			billList = billMapper.getBillList(productName, providerId, isPayment, currentPageNo, pageSize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeResource(connection, null, null);
		}
		return billList;

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean deleteBillById(String delId) {
		// TODO Auto-generated method stub
		Connection connection = null;
		boolean flag = false;
		try {
			connection = BaseDao.getConnection();
			if (billMapper.deleteBillById(delId) > 0)
				flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeResource(connection, null, null);
		}
		return flag;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public Bill getBillById(Integer id) {
		// TODO Auto-generated method stub
		Bill bill = null;
		Connection connection = null;
		try {
			connection = BaseDao.getConnection();
			bill = billMapper.getBillById(id);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			bill = null;
		} finally {
			BaseDao.closeResource(connection, null, null);
		}
		return bill;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean modify(Bill bill) {
		// TODO Auto-generated method stub
		Connection connection = null;
		boolean flag = false;
		try {
			connection = BaseDao.getConnection();
			if (billMapper.modify(bill) > 0)
				flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeResource(connection, null, null);
		}
		return flag;
	}

	@Override
	public int getBillCount(String productName, Integer providerId, Integer isPayment) {
		Connection connection = null;
		int count = 0;
		System.out.println("productName ---- > " + productName);
		System.out.println("providerId ---- > " + providerId);
		System.out.println("isPayment ---- > " + isPayment);
		try {
			connection = BaseDao.getConnection();
			count =billMapper.getBillCount(productName, providerId,isPayment);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return count;
	 
	}

	@Override
	public Bill selectBillsCodeExist(String billCode) {
		// TODO Auto-generated method stub
		Connection connection = null;
		Bill bill = null;
		try {
			connection = BaseDao.getConnection();
			bill = billMapper.selectBillCodeExist(billCode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return bill;
	}

}
